import flash.events.Event;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.KeyboardEvent;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFormat;

import CustomSprite;

import Images;
import Button;
class PopUpEvent extends Event{
	public function new(customEventString:String){

	super(customEventString, true, false);

    }
}

class PopUp extends CustomSprite
{
private var OKButton:Sprite;
public var text:TextField;
var mKeyDown:Array<Bool>;

  public function new(inBackground:BitmapData,Message:String,?BackgroundColor:Int=0xFFFFFF,?FrontColor:Int=0x000000)
  {
  super();
  var Background = new CustomSprite();
  Background.graphics.beginFill(BackgroundColor);
  Background.graphics.lineStyle(1,FrontColor);
  Background.graphics.drawRect(0, 0, Constants.flashWidth, Constants.flashHeight+6);
  Background.graphics.endFill();
  Background.alpha = 0.5;
  addChild(Background);
 
  var TextBackground = new CustomSprite();
  TextBackground.graphics.beginFill(BackgroundColor);
  TextBackground.graphics.lineStyle(1,FrontColor);
  TextBackground.graphics.drawRect(0, 0, 300, 200);
  TextBackground.x = 150;TextBackground.y = 150;
  addChild(TextBackground);
   
  text = new TextField();
  text.htmlText = Message;
  addChild(text);
  text.x = 200; text.y= 200;
  text.width = 250; text.height=200;
  text.wordWrap = true;
  text.selectable = false;
  text.mouseEnabled = false;

  OKButton = new Button(75,50,"OK",BackgroundColor,FrontColor);
  OKButton.x = 113;
  OKButton.y = 125;
  OKButton.buttonMode = true;
  OKButton.addEventListener(MouseEvent.CLICK, onOKButtonClick);
  TextBackground.addChild(OKButton);

  mKeyDown = [];

  this.addEventListener(Event.ADDED_TO_STAGE,onADD);
  }


public function onADD (event:Event)
{
  stage.focus = stage;
  stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown );
  stage.addEventListener(KeyboardEvent.KEY_UP, OnKeyUp );
  
}
public function OnKeyUp (event:KeyboardEvent)
    {
      mKeyDown[event.keyCode] = false;
    }
public function OnKeyDown(event:KeyboardEvent)
{
      if (!mKeyDown[event.keyCode])
      {
         mKeyDown[event.keyCode] = true;
	  if(mKeyDown[13] == true)
	  {
	    Back();
	  }
      }
      
}
public function Load()
{
  flash.Lib.current.addChild(this);
}
private function onOKButtonClick(event:MouseEvent) {
    Back();
}
  public function Back()
  {
    dispatchEvent(new PopUpEvent("PopUp"));
    //flash.Lib.current.removeChild(this);

  }



}